// eslint-disable-next-line @typescript-eslint/no-var-requires
const fs = require('fs')
const chalk = require('chalk')
const inquirer = require('inquirer')
const ora = require('ora')
// eslint-disable-next-line @typescript-eslint/no-var-requires
const path = require('path')
const basePath = path.resolve(__dirname, '../src')

module.exports = () => {

  // 获得module文件夹下所有的文件夹及文件
  const moduleFiles = fs.readdirSync(`${basePath}/module`).filter(item => item.indexOf('.') == -1)


  if (moduleFiles.length >= 1) {
    console.log(chalk.bold.cyan("src/module"), chalk.bold.magentaBright('路径下存在的文件夹有：'))
    console.log(chalk.bold.cyan(`${moduleFiles}`))
  }

  inquirer.prompt([{
    type: 'list',
    message: '请选择在那个模块下创建菜单:',
    name: 'moduleName',
    choices: moduleFiles
  }]).then(answers => {
    const moduleName = answers.moduleName

    const menuFiles = fs.readdirSync(`${basePath}/module/${moduleName}/page`).filter(item => item.indexOf('.') == -1)

    inquirer.prompt([{
      type: 'input', // 问题类型为填空题
      message: '您想要创建的菜单名称是(请输入英文):', // 问题描述
      name: 'menuName', // 问题对应的属性
      validate: (val) => { // 对输入的值做判断
        if (val === "") {
          return chalk.bold.red('模块名称不能为空，请重新输入')
        }
        if (menuFiles.indexOf(val) != -1) {
          console.log(chalk.bold.blue('以下是您现有的菜单名:'), chalk.bold.red(`${menuFiles}`))
          return chalk.bold.red('该模块名称可能已存在，请重新输入')
        }
        return true
      }
    }]).then(answers => {

      const menuName = answers.menuName

      inquirer.prompt([{
        type: 'input', // 问题类型为填空题
        message: '该菜单的中文名称是(请输入中文，项目启动后的名称):', // 问题描述
        name: 'routerMenuName', // 问题对应的属性
      }]).then(answers => {
        const routerMenuName = answers.routerMenuName === '' ? '菜单名' : answers.routerMenuName

        const apiTep = `import http from '@/base/api/public'
let service = require('@/../config/serviceList')
let sysConfig = require('@/../config/sysConfig')
let apiPre = sysConfig.apiPre

export default {
  /*
  示例api
  getData (params) {
    return http.requestGet(apiPre + service.服务名 + '/${menuName}/getData', params)
  },
  postData (params) {
    return http.requestPost(apiPre + service.服务名 + '/${menuName}/post', params)
  },
  delData (params) {
    return http.requestDelete(apiPre + service.服务名 + '/${menuName}/del', params)
  },
  putData (params) {
    return http.requestPut(apiPre + service.服务名 + '/${menuName}/put', params)
  },
  */
}`

        const crudTep = `import http from '@/base/api/public'
let service = require('@/../config/serviceList')
let sysConfig = require('@/../config/sysConfig')
let apiPre = sysConfig.apiPre

export default {
  getData (params) {
    return http.requestGet(apiPre + service.服务名 + '/${menuName}/getData', params)
  },
  postData (params) {
    return http.requestPost(apiPre + service.服务名 + '/${menuName}/post', params)
  },
  delData (params) {
    return http.requestDelete(apiPre + service.服务名 + '/${menuName}/del', params)
  },
  putData (params) {
    return http.requestPut(apiPre + service.服务名 + '/${menuName}/put', params)
  },
}`

        const vueTep = `<template>
  <Tabs :activeName="activeName" :tabsData="tabsData">
  <!-- npm run page Identifier:soleTep -->

  </Tabs>
</template>
      
<script>
import Tabs from '@/components/layout/tabs'
// npm run page Identifier:importTep
      
export default {
  name: 'manage',
  components: {
    Tabs,
    // npm run page Identifier:componentsTep
  },
  data () {
    return {
      activeName: '',
      tabsData: [
        // npm run page Identifier:activeTep

        // npm run page Identifier:tabsTep
      ]
    }
  },
  created () {
    this.activeName = this.tabsData[0].name
    // console.log('manage菜单创建啦')
  },
  mounted () {
    // console.log('manage菜单挂载啦')
  },
  watch: {},
  methods: {
    // handleClick (tab, event) {
    //   console.log(this.activeName)
    // }
  }
}
</script>

<style>
</style>
`

        // 路由文件加入菜单路由
        const addTep = `    {
      path: '${menuName}',
      component: () => import('@/module/${moduleName}/page/${menuName}/manage.vue'),
      meta: { title: '${routerMenuName}', icon: 'icon-order' }
    },`

        const spinner = ora().start()

        // 写入api文件
        process.chdir(`${basePath}/module/${moduleName}/api`) // cd api
        fs.writeFileSync(`${menuName}.js`, apiTep) // api.js
        spinner.succeed(`src/module/${moduleName}/api/${menuName}.js`)

        process.chdir(`${basePath}/module/${moduleName}/api/crud`) // cd api crud
        fs.writeFileSync(`${menuName}.js`, crudTep) // crud.js
        spinner.succeed(`src/module/${moduleName}/api/crud/${menuName}.js`)

        // 写入data
        process.chdir(`${basePath}/module/${moduleName}/data`) // cd data
        fs.mkdirSync(`./${menuName}`) // mkdir menuName
        spinner.succeed(`src/module/${moduleName}/data/${menuName}`)

        // 写入page
        process.chdir(`${basePath}/module/${moduleName}/page`) // cd page
        fs.mkdirSync(`./${menuName}`) // mkdir menuName
        spinner.succeed(`src/module/${moduleName}/page/${menuName}`)

        process.chdir(`${basePath}/module/${moduleName}/page/${menuName}`) // cd page menuName
        fs.writeFileSync('manage.vue', vueTep) // data.js
        spinner.succeed(`src/module/${moduleName}/page/${menuName}/manage.vue`)

        // 写入router
        process.chdir(`${basePath}/module/${moduleName}/router`) // cd page
        const readData = fs.readFileSync('./index.js', 'utf8').split(/\r\n|\n|\r/gm); //读取文件
        readData.splice(readData.length - 3, 0, addTep); //朝着读取的内容添加指定内容
        fs.writeFileSync('index.js', readData.join('\r\n')) //重新写入文件
        spinner.succeed(`src/module/${moduleName}/router/index.js`)

        require('./command.js')()





      })


    })





  })
}